package com.leo;

import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;

/**
 * @ClassName TableViewStudentProDemo
 * @Description
 * @Author wangss
 * @date 2020.06.13 20:35
 * @Version 1.0
 */
public class TableViewListenerDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #fff2f2");

        StudentPro s1 = new StudentPro("小a", 10, 50d, false);
        StudentPro s2 = new StudentPro("小b", 11, 65d, true);
        StudentPro s3 = new StudentPro("小c", 11, 74d, true);
        StudentPro s4 = new StudentPro("小d", 9, 98d, true);

        ObservableList<StudentPro> list = FXCollections.observableArrayList();
        list.addAll(s1, s2, s3, s4);

        TableView<StudentPro> tableView = new TableView<>(list);
//        tableView.setPlaceholder(new Label("没有数据"));

        tableView.setPrefWidth(400d);
        tableView.setPrefHeight(400d);

        TableColumn<StudentPro, String> name = new TableColumn<>("姓名");
        name.setCellValueFactory(new PropertyValueFactory<>("name"));

        TableColumn<StudentPro, Integer> age = new TableColumn<>("年龄");
        age.setCellValueFactory(new PropertyValueFactory<>("age"));

        TableColumn<StudentPro, Double> score = new TableColumn<>("成绩");
        score.setCellValueFactory(new PropertyValueFactory<>("score"));


        TableColumn<StudentPro, Boolean> flag = new TableColumn<>("是否及格");
        flag.setCellValueFactory(new PropertyValueFactory<>("flag"));

        tableView.getColumns().addAll(name, age, score, flag);

        Button button = new Button("button");

        AnchorPane.setTopAnchor(tableView, 50d);
        AnchorPane.setLeftAnchor(tableView, 50d);
        anchorPane.getChildren().addAll(button, tableView);

        Scene scene = new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.setWidth(800d);
        primaryStage.setHeight(800d);
        primaryStage.setTitle("TableView");
        primaryStage.setResizable(false);
        primaryStage.show();

        button.setOnAction(event -> {
            s1.setName("小改");
        });


        tableView.setTableMenuButtonVisible(true);
        // 列宽
        tableView.setFixedCellSize(60d);

        tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

        // 允许编辑
        tableView.setEditable(true);
        name.setCellFactory(TextFieldTableCell.forTableColumn());

        age.setCellFactory(TextFieldTableCell.forTableColumn(new StringConverter<Integer>() {
            @Override
            public String toString(Integer object) {
                return object.toString();
            }

            @Override
            public Integer fromString(String string) {
                return Integer.parseInt(string);
            }
        }));

//        tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        tableView.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {

            }
        });
        tableView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<StudentPro>() {
            @Override
            public void changed(ObservableValue<? extends StudentPro> observable, StudentPro oldValue, StudentPro newValue) {

            }
        });

        tableView.getSelectionModel().getSelectedCells().addListener(new InvalidationListener() {
            @Override
            public void invalidated(Observable observable) {
                ObservableList<TablePosition> obs = (ObservableList<TablePosition>) observable;
                for (int i = 0; i < obs.size(); i++) {
                    TablePosition tablePosition = obs.get(i);
                    Object cellData = tablePosition.getTableColumn().getCellData(tablePosition.getRow());
                    System.out.println("行 = " + tablePosition.getRow() + " 列 = " + tablePosition.getColumn() + " 数据 = " + cellData.toString());
                }
            }
        });
    }
}
